package com.xh.controller.data;

import com.xh.service.BlogService;
import com.xh.service.PraiseService;

import com.xh.vo.Praise;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("Praise")
public class PraiseController {
    @Resource
    private PraiseService praiseService;
    @Resource
    private BlogService blogService;

    @GetMapping("newPraise/{userId}/{Bid}")
    public String newPraise(@PathVariable String userId,
                            @PathVariable String Bid) {
        int i1 = Integer.parseInt(userId);
        int i2 = Integer.parseInt(Bid);
        Praise praise = new Praise(i1, i2, 1);
        List<Praise> praises = praiseService.queryPraise(i1, i2);
        List<Praise> praises2 = praiseService.queryPraise2(i1, i2);
        if (praises2.isEmpty()) {
            praiseService.newPraise(praise);
            blogService.newParis(i2);
        }else {
            if (praises.isEmpty()){
                praiseService.updatePraise(1,i1,i2);
                blogService.newParis(i2);
            }else {
                blogService.deleteParis(i2);
                praiseService.updatePraise(0,i1,i2);
            }
        }
        return "redirect:/toBlogContent/"+Bid+"/"+userId;          //   /toBlogContent/{Bid}/{userId}
    }
}
